//
//  CircleHomeViewController.m
//  DefendHealth
//
//  Created by UNOHACHA on 17/2/18.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "CircleHomeViewController.h"
#import "SGTopTitleView.h"
#import "HotCircleViewController.h"
#import "FocusCircleViewController.h"
#import "IssuedByTheDynamicViewController.h"
#import "LittleVideoController.h"
@interface CircleHomeViewController ()<SGTopTitleViewDelegate,UIScrollViewDelegate,UITextFieldDelegate>
{
    UIView *backView1;
    UIButton *releaseBtn;
}
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) HotCircleViewController *arDyVC ;
@property (nonatomic, strong) SGTopTitleView *topTitleView;
@end

@implementation CircleHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   self.titles = @[@"热门", @"关注"];
    self.leftbutton.hidden = YES;
    [self addNavView];
    //1. 创建滑动View
    [self topTitiViewInit];
    //2.添加所有子控制器
    [self setupChildViewController];
    //3.初始化scrollview
    [self.view addSubview:self.mainScrollView];
    
    
    [_mainScrollView addSubview:_arDyVC.view];
    [self.view insertSubview:_mainScrollView belowSubview:_topTitleView];
    UIView *hui = [UIView new];
    hui.frame = CGRectMake(0, 64,WindowWidth , 0.5);
    hui.backgroundColor = kLineColor;
    [self.view addSubview:hui];
    
    [self addBackView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addNavView{
    UIButton *addFriendsBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, [ToolUtil autoWidth:@"添加好友" width:500 height:13 num:13], 44)];
    [addFriendsBtn setTitle:@"添加好友" forState:UIControlStateNormal];
    [addFriendsBtn addTarget:self action:@selector(addFriends) forControlEvents:UIControlEventTouchUpInside];
    addFriendsBtn.titleLabel.font = FONT(13);
    [addFriendsBtn setTitleColor:[ImageUtil colorWithRgb:0x333333] forState:0];
    
    [self.view addSubview:addFriendsBtn];
    
    releaseBtn = [[UIButton alloc] initWithFrame:CGRectMake(WindowWidth-10-[ToolUtil autoWidth:@"发布" width:500 height:13 num:13], 20, [ToolUtil autoWidth:@"发布" width:500 height:13 num:13], 44)];
    [releaseBtn setTitle:@"发布" forState:UIControlStateNormal];
    [releaseBtn addTarget:self action:@selector(release:) forControlEvents:UIControlEventTouchUpInside];
    releaseBtn.titleLabel.font = FONT(13);
    releaseBtn.titleLabel.textAlignment = 2;
    [releaseBtn setTitleColor:[ImageUtil colorWithRgb:0x333333] forState:0];
    [self.view addSubview:releaseBtn];
}
-(void)addFriends{
    
}

-(void)release:(UIButton *)btn{
    releaseBtn.hidden = YES;
    backView1.hidden = NO;
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        // 创建底部滚动视图
        _mainScrollView = [[UIScrollView alloc] init];
        _mainScrollView.frame = CGRectMake(0, kMaxY(_topTitleView.frame),kScreenWidth, kScreenHeight-kMaxY(_topTitleView.frame));
        _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
        _mainScrollView.backgroundColor = [UIColor clearColor];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.bounces = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.delegate = self;
        _mainScrollView.scrollEnabled = NO;
        
    }
    return _mainScrollView;
}

-  (void)topTitiViewInit {
    
    self.topTitleView = [SGTopTitleView topTitleViewWithFrame:CGRectMake((WindowWidth-100)/2, 20, 100, 44)];
    _topTitleView.staticTitleArr = [NSArray arrayWithArray:_titles];
    _topTitleView.delegate_SG = self;
    _topTitleView.layer.shadowColor=[HOME_COLOR colorWithAlphaComponent:0.8].CGColor;
    _topTitleView.layer.shadowOffset=CGSizeMake(0,40);
    
    _topTitleView.layer.shadowOpacity=0.5;
    _topTitleView.backgroundColor = [UIColor whiteColor];
    _topTitleView.layer.shadowRadius=8;
    [self.view addSubview:_topTitleView];
}

#pragma mark - - - SGTopScrollMenu代理方法
- (void)SGTopTitleView:(SGTopTitleView *)topTitleView didSelectTitleAtIndex:(NSInteger)index{
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * kScreenWidth;
    _mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}
#pragma mark - -  显示控制器的view
- (void)showVc:(NSInteger)index {
    UIViewController *vc = self.childViewControllers[index];
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    
    [_mainScrollView addSubview:vc.view];
    vc.view.x = index * kScreenWidth;
}

#pragma mark - -   添加所有子控制器

- (void)setupChildViewController {
    // 热门
    HotCircleViewController *arDyVC = [[HotCircleViewController alloc] init];
    _arDyVC = arDyVC;
    [self addChildViewController:arDyVC];
    
    // 关注
    FocusCircleViewController *arSuVC = [[FocusCircleViewController alloc] init];
    [self addChildViewController:arSuVC];
    
}

-(void)addBackView{
    backView1 = [UIView new];
    backView1.frame = CGRectMake(0, 0,WindowWidth , WindowHeight);
    backView1.backgroundColor = [UIColor whiteColor];
    backView1.alpha = 0.8;
    backView1.hidden = YES;
    [self.view addSubview:backView1];
    
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame: CGRectMake(WindowWidth-100-50, 20,50, 50)];
    [btn1 setTitle:@"图文" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(tuwen) forControlEvents:UIControlEventTouchUpInside];
    btn1.titleLabel.font = FONT(11);
    btn1.layer.cornerRadius = 25;
    btn1.layer.masksToBounds = YES;
    btn1.backgroundColor = [UIColor blackColor];
    [btn1 setTitleColor:[ImageUtil colorWithRgb:0xffffff] forState:0];
    [backView1 addSubview:btn1];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame: CGRectMake(kMaxX(btn1.frame)+17, 80,50, 50)];
    [btn2 setTitle:@"小视频" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(xiaoshipin) forControlEvents:UIControlEventTouchUpInside];
    btn2.titleLabel.font = FONT(11);
    btn2.layer.cornerRadius = 25;
    btn2.layer.masksToBounds = YES;
    btn2.backgroundColor = [UIColor blackColor];
    [btn2 setTitleColor:[ImageUtil colorWithRgb:0xffffff] forState:0];
    [backView1 addSubview:btn2];
    
    UIButton *cancelbtn1 = [[UIButton alloc] initWithFrame:CGRectMake(WindowWidth-10-[ToolUtil autoWidth:@"取消" width:500 height:13 num:13], 20, [ToolUtil autoWidth:@"取消" width:500 height:13 num:13], 40)];
    [cancelbtn1 setTitle:@"取消" forState:UIControlStateNormal];
    [cancelbtn1 addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    cancelbtn1.titleLabel.font = FONT(13);
    
    [cancelbtn1 setTitleColor:[UIColor blackColor] forState:0];
    
    [backView1 addSubview:cancelbtn1];
}
-(void)tuwen{
    IssuedByTheDynamicViewController *stVC = [IssuedByTheDynamicViewController new];
    stVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:stVC animated:YES];
}
-(void)xiaoshipin{
    LittleVideoController *stVC = [LittleVideoController new];
    stVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:stVC animated:YES];
}
-(void)quxiao{
    backView1.hidden = YES;
    releaseBtn.hidden = NO;
}
@end
